﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Cors;
using AssetsSys.BLL;
using AssetsSys.Dto.Type;
using AssetsSys.WebApi.Filters;
using AssetsSys.WebApi.Models.Type;

namespace AssetsSys.WebApi.Controllers
{
    /// <summary>
    /// 资产类型
    /// </summary>
    [MyAuth]
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    [RoutePrefix("api/types")]
    public class TypeController : ApiController
    {
        /// <summary>
        /// 类型列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("infos")]
        public async Task<IHttpActionResult> GetTypeList()
        {
            return this.SendData(await TypeManger.GetList());
        }

        /// <summary>
        /// 添加类型
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("infos")]
        public async Task<IHttpActionResult> CreateType(CreateTypeViewModel model)
        {
            if (!ModelState.IsValid) return this.ErrorData(ModelState.Values.FirstOrDefault()?.Errors[0].ErrorMessage);
            return await TypeManger.Create(model.Code,model.TypeName,model.AgeLimit) ? this.SendData("添加成功") : this.ErrorData("添加失败");
        }

        /// <summary>
        /// 获取类型信息
        /// </summary>
        /// <param name="typeid">类型Id</param>
        /// <returns></returns>
        [HttpGet]
        [Route("{typeid}/infos")]
        public async Task<IHttpActionResult> GetType(Guid typeid)
        {
            return this.SendData(await TypeManger.GetById(typeid));
        }

        /// <summary>
        /// 更新类型名称
        /// </summary>
        /// <param name="typeid">类型Id</param>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut]
        [Route("{typeid}/infos")]
        public async Task<IHttpActionResult> UpdateType(Guid typeid, TypeModel model)
        {
            model.Id = typeid;
            return await TypeManger.Update(model) ? this.SendData("修改成功") : this.ErrorData("修改失败");
        }

        /// <summary>
        /// 删除类型
        /// </summary>
        /// <param name="typeid">类型Id</param>
        /// <returns></returns>
        [HttpDelete]
        [Route("{typeid}/infos")]
        public async Task<IHttpActionResult> DeleteType(Guid typeid)
        {
            return await TypeManger.Delete(typeid) ?this.SendData("删除成功"): this.ErrorData("删除失败");
        }

        /// <summary>
        /// 获取资产类型统计信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("countinfo")]
        public async Task<IHttpActionResult> GetCountInfo()
        {
            var data = await TypeManger.GetCountInfo();
            return this.SendData(data);
        }
    }
}
